#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int n,h[500010],stk[500010],top;
long long ans;

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &h[i]);

    for (int i = 1; i <= n; i++)
    {
        while (top != 0 && stk[top] <= h[i])
            top--;
        ans += top;
        stk[++top] = h[i];
    }
    printf("%lld", ans);

    return 0;
}
